#include "./code/optionPage/setWifi/WifiKeyBoard.h"
#include <QDebug>

WifiKeyBoard::WifiKeyBoard(QWidget *parent) :
    QWidget(parent)
{
    setupUi(this);

    //初始化数字和字母按键组，注意通过text长度为1判断
    m_letterKeyGroup = new QButtonGroup(this);
    foreach(QObject *obj, page->children())
    {
        if (obj->inherits("QPushButton")) {
            QPushButton* b = qobject_cast<QPushButton *>(obj);
            if(b->text().size()==1)
            {
                m_letterKeyGroup->addButton(b);
                qDebug()<<"l:"<<b->text();
            }
        }
    }
    connect(m_letterKeyGroup,SIGNAL(buttonClicked(QAbstractButton*)),
            this,SLOT(on_inputButton_cliicked(QAbstractButton*)));

    //初始化符号按键组,注意通过text长度为1判断
    m_symbolKeyGroup = new QButtonGroup(this);
    foreach(QObject *obj, page_2->children())
    {
        if (obj->inherits("QPushButton")) {
            QPushButton* b = qobject_cast<QPushButton *>(obj);
            if(b->text().size()==1)
            {
                m_symbolKeyGroup->addButton(b);
                qDebug()<<"s:"<<b->text();
            }
        }
    }
    connect(m_symbolKeyGroup,SIGNAL(buttonClicked(QAbstractButton*)),
            this,SLOT(on_inputButton_cliicked(QAbstractButton*)));
}

//大小写
void WifiKeyBoard::on_pushButton_41_clicked(bool checked)
{
    if(checked)
    {
        foreach(QAbstractButton *button, m_letterKeyGroup->buttons())
        {
            button->setText(button->text().toUpper());
        }
    }else
    {
        foreach(QAbstractButton *button, m_letterKeyGroup->buttons())
        {
            button->setText(button->text().toLower());
        }
    }
}

//删除键
void WifiKeyBoard::on_pushButton_144_clicked()
{
    lineEdit->setText(lineEdit->text().mid(0,lineEdit->text().size()-1));
}

//删除键
void WifiKeyBoard::on_pushButton_40_clicked()
{
    lineEdit->setText(lineEdit->text().mid(0,lineEdit->text().size()-1));
}

void WifiKeyBoard::on_pushButton_37_clicked()
{
    lineEdit->setText(lineEdit->text()+" ");
}

//字符和字母切换
void WifiKeyBoard::on_pushButton_38_clicked(bool checked)
{
    if(checked)
    {
        pushButton_38->setText("abc");
        stackedWidget->setCurrentWidget(page_2);
    }else
    {
        pushButton_38->setText("#+=");
        stackedWidget->setCurrentWidget(page);
    }
}

//字符、数字、字母输入
void WifiKeyBoard::on_inputButton_cliicked(QAbstractButton * button )
{
    lineEdit->setText(lineEdit->text()+ button->text());
}

//ok点击后发送密码输入完成信号
void WifiKeyBoard::on_pushButton_39_clicked()
{
    emit has_input_password(lineEdit->text());
}
